package com.xbxkj.commen.serviceImpl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbxkj.commen.bean.ShopExamine;
import com.xbxkj.commen.bean.ShopShops;
import com.xbxkj.commen.mapper.ShopExamineMapper;
import com.xbxkj.commen.service.ShopExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbxkj.commen.bean.layuiBean.ResultPage;
import com.xbxkj.commen.controller.model.ApiResultModel;
import com.xbxkj.commen.controller.helper.$;
import com.xbxkj.commen.service.ShopShopsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xbxkj.commen.controller.helper.V;
import com.xbxkj.commen.controller.helper.X;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2020-01-09
 */
@Service
public class ShopExamineServiceImpl extends ServiceImpl<ShopExamineMapper, ShopExamine> implements ShopExamineService {
    /**
     * 添加ShopExamine  如果存在 就修改
     *
     * @param shopExamine 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> addShopExamine(ShopExamine shopExamine) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", shopExamine.getUserId());
        ShopExamine databaseShopEx = getOne(queryWrapper);
        if (databaseShopEx != null) {
            int id = databaseShopEx.getId();
            databaseShopEx = shopExamine;
            databaseShopEx.setId(id);
            if (this.updateById(shopExamine)) {
                return $.success();
            } else {
                return $.error("修改失败");
            }
        }
        if (this.save(shopExamine)) {
            return $.success();
        } else {
            return $.error("添加失败");
        }
    }

    /**
     * 编辑跳转页面的回显数据ShopExamine
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public ShopExamine selectShopExamineByIdShopExamine(String id) {
        return this.getById(id);
    }

@Autowired
    ShopShopsService shopShopsService;
    /**
     * 编辑ShopExamine
     *
     * @param shopExamine 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> editShopExamine(ShopExamine shopExamine) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",shopExamine.getUserId());
        if(shopShopsService.getOne(queryWrapper)!=null){
            return $.error("错误，该商家已审核通过，并且已入驻，请联系商家更换微信账户再试。");
        }

        if (this.updateById(shopExamine)) {
            shopExamine=getById(shopExamine.getId());
            if(shopExamine.getExamineStatus()==1){//如果是审核通过  那么就把审核的信息 加到 商家信息里

                ShopShops shopShops=new ShopShops();
                shopShops.setShopSn(UUID.randomUUID().toString());
                shopShops.setShopType(shopExamine.getShopType());
                shopShops.setUserId(shopExamine.getUserId());
                shopShops.setTelePhone(shopExamine.getShopTel());
                shopShops.setShopKeeper(shopExamine.getShopKeeper());
                shopShops.setShopAddress(shopExamine.getShopAddress());
                shopShops.setCreateTime(System.currentTimeMillis());
                shopShops.setShopMoney("0");
                shopShops.setLockMoney("0");
                shopShops.setShopStatus(shopExamine.getExamineStatus());
                shopShopsService.save(shopShops);
            }
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    /**
     * 删除一条ShopExamine
     *
     * @param id 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> delShopExamine(String id) {
        if (this.removeById(id)) {
            return $.success();
        } else {
            return $.error("删除失败");
        }
    }

    /**
     * 分页查询ShopExamine
     *
     * @param page        页码
     * @param limit       查几条
     * @param shopExamine 前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findShopExamineByPage(Integer page, Integer limit, ShopExamine shopExamine) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("shopExamine", shopExamine);
        List<ShopExamine> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<ShopExamine> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
}
